package db

import (
	"CodeCover/internal/config"
	mylog "CodeCover/internal/log"
	"fmt"

	"go.mongodb.org/mongo-driver/v2/mongo"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

var (
	SqlDB   *gorm.DB
	MongoDB *mongo.Client
)

func InitDB(cfg *config.AllConfig) {

	// mysql
	dst := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		cfg.DBs.MysqlCfg.Username, cfg.DBs.MysqlCfg.Password,
		cfg.DBs.MysqlCfg.IP, cfg.DBs.MysqlCfg.Port, cfg.DBs.MysqlCfg.Database)

	var err error
	SqlDB, err = gorm.Open(mysql.Open(dst), &gorm.Config{
		// Logger: &mylog.LogrusGormLogger{
		// 	Logger:                    mylog.Logger,
		// 	SlowThreshold:             1 * time.Second,
		// 	LogLevel:                  gormlogger.Info,
		// 	IgnoreRecordNotFoundError: true,
		// },
		// Logger: mylog.NewLogrusGormLogger(mylog.Logger),
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		mylog.Logger.Fatalf("Failed to connect to database, error:%v", err)
	}

	// mongodb
	// mgCfg := cfg.DBs.MongodbCfg
	// uri := fmt.Sprintf("mongodb://%s:%s@%s:%d/", mgCfg.Username, mgCfg.Password, mgCfg.IP, mgCfg.Port)
	// serverAPI := options.ServerAPI(options.ServerAPIVersion1)
	// opts := options.Client().ApplyURI(uri).SetServerAPIOptions(serverAPI)
	// MongoDB, err = mongo.Connect(opts)
	// if err != nil {
	// 	mylog.Logger.Fatalf("Failed to connect to database: error:%v", err)
	// }

}
